package data.structure.sort;

/**
 * @ClassName: Sort
 * @Description:
 * @Author: qiaopengxiang
 * @Date: 2022/7/7 11:19
 */
public class Sort {

    /**
     * 冒泡排序
     */
    public void maopao(int[] array){
        if (array==null || array.length<=1){
            return;
        }
        for (int i =0;i<array.length;i++){
            boolean flag = true;
            for (int j = 0;j<array.length-i-1;j++){
                if (array[j]>array[j+1]){
                    int temp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = temp;
                    flag = false;
                }
            }
            if (flag){
                break;
            }
        }
    }

    /**
     * 插入排序
     */
    public void insert(int[] array){
       if (array==null || array.length<2){
           return;
       }
       for (int i =0;i<array.length;i++){
           int x = array[i];
           int j = i-1;
           while (j>=0 && array[j]>x){
               array[j+1] = array[j];
               j--;
           }
           array[j+1] = x;
       }
    }
}
